
import os
import tkinter as tk
from tkinter import filedialog, messagebox
import pandas as pd

class XlsToCsvConverter:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("XLS转CSV转换器")
        self.root.geometry("500x220")
        
        self.setup_ui()
        
    def setup_ui(self):
        # 输入路径提示
        tk.Label(self.root, text="输入xls/xlsx文件的根目录", 
                fg="red", font=("Arial", 9)).pack(pady=(10,0))
        
        # 输入路径行
        input_frame = tk.Frame(self.root)
        input_frame.pack(pady=5, padx=20, fill=tk.X)
        
        tk.Label(input_frame, text="输入路径:").pack(side=tk.LEFT)
        self.input_entry = tk.Entry(input_frame, width=40)
        self.input_entry.pack(side=tk.LEFT, padx=5)
        
        tk.Button(input_frame, text="浏览...", command=self.select_input).pack(side=tk.LEFT)
        
        # 输出路径提示
        tk.Label(self.root, text="输入csv的输出目录", 
                fg="red", font=("Arial", 9)).pack(pady=(10,0))
        
        # 输出路径行
        output_frame = tk.Frame(self.root)
        output_frame.pack(pady=5, padx=20, fill=tk.X)
        
        tk.Label(output_frame, text="输出路径:").pack(side=tk.LEFT)
        self.output_entry = tk.Entry(output_frame, width=40)
        self.output_entry.pack(side=tk.LEFT, padx=5)
        
        tk.Button(output_frame, text="浏览...", command=self.select_output).pack(side=tk.LEFT)
        
        # 确定按钮
        confirm_frame = tk.Frame(self.root)
        confirm_frame.pack(side=tk.BOTTOM, pady=10, fill=tk.X)
        tk.Button(confirm_frame, text="确定", width=15, command=self.convert_xls_to_csv).pack()
    
    def select_input(self):
        path = filedialog.askdirectory(title="选择输入文件夹")
        if path: 
            self.input_entry.delete(0, tk.END)
            self.input_entry.insert(0, path)
    
    def select_output(self):
        path = filedialog.askdirectory(title="选择输出文件夹")
        if path: 
            self.output_entry.delete(0, tk.END)
            self.output_entry.insert(0, path)
    
    def convert_xls_to_csv(self):
        input_path = self.input_entry.get()
        output_path = self.output_entry.get()
        
        if not input_path or not output_path:
            messagebox.showerror("错误", "请选择输入和输出路径")
            return
        
        try:
            xls_files = [f for f in os.listdir(input_path) 
                        if f.endswith(('.xls', '.xlsx'))]
            
            if not xls_files:
                messagebox.showwarning("警告", "输入路径中没有找到XLS/XLSX文件")
                return
            
            for xls_file in xls_files:
                xls_path = os.path.join(input_path, xls_file)
                df = pd.read_excel(xls_path)
                
                csv_file = os.path.splitext(xls_file)[0] + '.csv'
                csv_path = os.path.join(output_path, csv_file)
                df.to_csv(csv_path, index=False, encoding='utf_8_sig')
            
            messagebox.showinfo("完成", f"成功转换 {len(xls_files)} 个Excel文件")
            self.root.destroy()
            
        except Exception as e:
            messagebox.showerror("错误", f"转换过程中发生错误: {str(e)}")

if __name__ == "__main__":
    app = XlsToCsvConverter()
    app.root.mainloop()
